package itsm.isperp.module.utils;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.domain.IUser;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.module.entity.app.AppUser;
import itsm.isperp.module.entity.bpm.BpmActionHistory;
import itsm.isperp.module.entity.db.DbEntity;
import itsm.isperp.module.service.app.AppRoleService;
import itsm.isperp.module.service.app.AppUserService;
import itsm.isperp.module.service.bpm.BpmActionHistoryService;
import itsm.isperp.module.service.bpm.BpmProcessSettingService;
import itsm.isperp.module.service.project.ProjProjectMemberService;

import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.JdbcTemplate;

/**
 * 
 * 
 * 
 * @author lizx
 * @date 2014-9-3
 * @since 1.0
 * 
 */
public class MessageUserUtils {

	public static final void addToReceiver(Map<String, IUser> mapUsers,
			final Map<String, Object> user, String businessKey,
			String projectId, String processKey) {
		AppRoleService appRoleService = ContextHolder
				.getSpringBean("appRoleService");
		AppUserService appUserService = ContextHolder
				.getSpringBean("appUserService");
		JdbcTemplate jdbcTemplate = ContextHolder.getSpringBean("jdbcTemplate");

		BpmProcessSettingService setting = ContextHolder
				.getSpringBean("bpmProcessSettingService");
		String cmpIds = user.get("cmp_ids").toString();
		int at = Integer.valueOf(user.get("assign_type").toString());
		switch (at) {
		case 0:
			// 通过查询获取发起人
			BpmActionHistoryService bpmActionHistoryService = ContextHolder
					.getSpringBean("bpmActionHistoryService");
			BpmActionHistory bh = bpmActionHistoryService
					.findStartByBusinessKey(businessKey);
			if (!mapUsers.containsKey(bh.getActionUserId())) {
				IUser us = appUserService.findByUsername(bh.getActionUserId());

				if (!mapUsers.containsKey(us.getUsername())) {
					mapUsers.put(us.getUsername(), us);
				}
			}

			break;

		case 1:
			// 按角色

			List<? extends IUser> us = appRoleService.findRoleMembers(cmpIds);
			for (IUser u : us) {
				if (!mapUsers.containsKey(u.getUsername())) {
					mapUsers.put(u.getUsername(), u);
				}
			}
			break;

		case 11:
			// 项目中的角色成员

			ProjProjectMemberService projProjectMemberService = ContextHolder
					.getSpringBean("projProjectMemberService");

			us = projProjectMemberService.findMemberUserByProjectIdAndRole(
					projectId, cmpIds);

			for (IUser u : us) {
				if (!mapUsers.containsKey(u.getUsername())) {
					mapUsers.put(u.getUsername(), u);
				}
			}
			break;
		case 2:
			// 指定人员
			IUser u = appUserService.findByUsername(cmpIds);
			if (!mapUsers.containsKey(u.getUsername())) {
				mapUsers.put(u.getUsername(), u);
			}
			break;
		case 3:
		case 31:
			// 表单内特定成员--使用帐号保存

			DbEntity entity = setting.findDbEntityByKey(processKey);

			String sql = "SELECT " + cmpIds + " FROM " + entity.getTableName()
					+ " WHERE id=?";

			Map<String, Object> result = jdbcTemplate.queryForMap(sql,
					businessKey);
			if (result == null || result.size() == 0) {
				return;
			}

			String uName = result.get(cmpIds).toString();

			JqGridRequest req = new JqGridRequest();
			req.isPageable(false);
			String field = "id";
			if (at == 3) {
				field = "name";
			}

			if (uName.contains(",")) {
				String uNames = "'" + uName.replaceAll(",", "','") + "'";
				req.addFilterRule(field, JqGridFilterRuleOp.in, uNames);

			} else {
				req.addFilterRule(field, JqGridFilterRuleOp.eq, uName);
			}

			List<AppUser> vus = appUserService.findAll(req);

			for (AppUser vu : vus) {
				if (!mapUsers.containsKey(vu.getUsername())) {
					mapUsers.put(vu.getUsername(), vu);
				}

			}
			break;
		case 4:
			// 部门
			List<? extends IUser> userList = appUserService
					.findByDeptId(cmpIds);
			for (IUser uu : userList) {
				if (!mapUsers.containsKey(uu.getUsername())) {
					mapUsers.put(uu.getUsername(), uu);
				}
			}
			break;

		}

	}
}
